#include "paint.h"
#include "core.h"
#include "..\win\winfactory.h"

FacePainterImpl::FacePainterImpl()
    : canvas_( 0 )
{
}

FacePainterImpl::~FacePainterImpl()
{
}

void FacePainterImpl::SetContext( core::canvas canvas )
{
    canvas_ = canvas;
}

FacePainter::FacePainter( core::canvas canvas ) 
    : impl_( 0 )
    , canvas_( canvas )
{
}

FacePainter::~FacePainter()
{
    if( impl_ )
        delete impl_;
}


int FacePainter::SetClipRect
              (
                const core::rect &rect
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();
    if( impl )
    {
        return impl->SetClipRect( rect );
    }

    return 0;
}

bool FacePainter::ClearClipRect( int clip )
{
    FacePainterImpl *impl = GetFacePainterImpl();
    if( impl )
    {
        return impl->ClearClipRect( clip );
    }

    return false;
}

void FacePainter::DrawBorder
              (
                const core::rect &rect,
                core::dword border_style,
                int border_width,
                core::color border_color,
                int alpha
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();
    if( impl )
    {
        core::rect rcDraw = rect;
        for( int i = 0; i < border_width; i++ )
        {
            impl->DrawRectangle( rcDraw, border_style, border_color, alpha );
            rcDraw.deflate( 1, 1, 1, 1 );
        }
    }
}

void FacePainter::DrawBackground
              (
                const core::rect &rect,
                core::color background_color,
                int alpha
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();
    if( impl )
    {
        impl->FillRectangle( rect, background_color, alpha );
    }
}

void FacePainter::DrawImage
              (
                const std::wstring &file,
                const core::rect &rect,
                int alpha
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();

    if( impl )
        impl->DrawImage( file, rect, alpha );
}

void FacePainter::DrawGridImage
              (
                const std::wstring &file,
                const core::rect &rect,
                const core::rect &grid,
                int length,
                int index,
                int alpha
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();

    if( impl )
        impl->DrawGridImage( file, rect, grid, length, index, alpha );
}

void FacePainter::DrawString
              (
                const std::wstring &text,
                const core::rect &rect,
                const std::wstring &family,
                const core::unit &size,
                core::dword format,
                core::color color,
                core::dword align,
                int alpha
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();

    if( impl )
    {
        impl->SetTextFont( family, size, format );
        impl->DrawString( text, rect, color, align, alpha );
    }
}

int FacePainter::GetCharacterPosition
              (
                const std::wstring &text,
                int &width,
                const std::wstring &family,
                const core::unit &size,
                core::dword format
               )
{
    FacePainterImpl *impl = GetFacePainterImpl();

    if( impl )
    {
        impl->SetTextFont( family, size, format );
        return impl->GetCharacterPosition( text, width );
    }

    return 0;
}

FacePainterImpl * FacePainter::GetFacePainterImpl()
{
    if( !impl_ )
    {
        impl_ = WindowsFactory::Instance().GetPainterImpl();
        if( impl_ )
            impl_->SetContext( canvas_ );
    }

    return impl_;
}